package cn.kaiux.bigdata.homework.week05_06

import org.apache.hadoop.fs.{FileStatus, Path}

import java.net.URI

/**
 * @author kaiux@foxmail.com
 * @date 2022/6/11 11:56
 */
case class FileStatusWithType(uri: URI, len: Long, fileType: FileType) extends Serializable {

  def getPath: Path = new Path(uri)

  def getLen: Long = len

  def isDirectory: Boolean = fileType == Directory

  def isFile: Boolean = fileType == File
}

object FileStatusWithType {

  def apply(fileStatus: FileStatus): FileStatusWithType = {

    val fileType =
      if (fileStatus.isDirectory) Directory
      else if (fileStatus.isFile) File
      else throw new RuntimeException(s"File [$fileStatus] is not supported, only support file and directory")
    new FileStatusWithType(fileStatus.getPath.toUri, fileStatus.getLen, fileType)

  }
}

sealed trait FileType extends Serializable

case object File extends FileType

case object Directory extends FileType